/** @jsxImportSource @emotion/react */
import * as React from "react";
import {
    Text, Table, TableBody, TableRow, Cell, Button, Collapse, TableHead, useTheme, CCell,
} from "customize-easy-ui-component";
import { DirectLink } from "../../../routing/Link";
import {ReportViewProps, ReportViewSubProps,} from "../../common/base";
import {分项末尾链接,} from "../../common/rarelyVary";
import {useMainRepUrlOr, useSplitSubCapacity, useSplitSubRepList} from "../../hook/useMainRepUrlOr";
import {useContext} from "react";
import RoutingContext from "../../../routing/RoutingContext";
import {useThreeColumnSubr} from "../../hook/useThreeColumnSubr";
import {config测量, config测量仪,} from "./Detection.O-1";
import {useRaftTableViewer,} from "../../hook/useRepTableEditor";
import {TailMenRowIspCheck} from "../../contain/rarelyVary";

export const ReportView: React.FunctionComponent<ReportViewProps> = (
    {repId,   source: parentOrc,  verId,rep, redId,fxIdx ,...other}
) => {
    const {urlMainRep}=useMainRepUrlOr(rep);
    const lsBlockMax=useSplitSubCapacity(parentOrc?._OXIDESKIN_THICK?.length,1);       //【模板敲定一次性最大几个分项显示出来】有些分项报告模板可能存在很多个分项 最多1个
    const {sumArea, areaIdAs, btnBindUses}=useSplitSubRepList(parentOrc?._OXIDESKIN_THICK ?? [], lsBlockMax,false);
    const { get:getRouteUrl } = useContext(RoutingContext);
    const routeLevels=getRouteUrl().entries.length;
    const routeTemplate=getRouteUrl().entries[routeLevels-1]?.routeData?.params?.template;
    return (
        <React.Fragment>
          <div  id="rp_oxideskin_thick_"  css={{"@media not print": {marginTop:'1rem', marginBottom: '1rem'},}}>
             <DirectLink  href={`/report/OXIDESKIN_THICK/ver/${verId}/${repId}/OXIDESKIN_THICK/_Controller`}>
                <div css={{"@media print":{display: 'none'} }}>
                    <Button>{(repId===rep?.id && rep?.modeltype!=='OXIDESKIN_THICK')? '内嵌式的':'独立流转的'}管壁氧化皮厚度报告分项控制器</Button>
                </div>
             </DirectLink>
            {(new Array(sumArea).fill(null)).map((s:any,ak:number) => {
                const [isDisplay, bindBtn]=btnBindUses[ak];
                return <React.Fragment key={ak}>
                    <div key={ak} role="button" {...bindBtn}  css={{"@media print":{display: 'none'}}}>
                        { <Text variant="h4">{`管壁氧化皮厚度折叠区${ak+1}，`}{isDisplay ? `收起`:`更多..`}</Text> }
                    </div>
                    <Collapse id={`${ak}`} show={isDisplay} noAnimated>
                        { areaIdAs[ak]?.map((surpId: any, m:number) => {
                            const dispIdx=(fxIdx??0) +(m+ ak*lsBlockMax);
                            return <OneSubRepView key={m} fxIdx={fxIdx} dispIdx={dispIdx} rep={rep} repId={repId}
                                                  verId={verId} parentOrc={parentOrc} surpId={surpId}/>
                        }) }
                        <div role="button" {...bindBtn}  css={{"@media print":{display: 'none'}}}>
                            <Text variant="h4">{`管壁氧化皮厚度折叠区${ak+1}结束,收起`}</Text>
                        </div>
                    </Collapse>
                </React.Fragment>
            })}
          </div>
          {分项末尾链接({template:'OXIDESKIN_THICK', repId: repId||'',verId,urlMainRep,
                nestIn:'OXIDESKIN_THICK'!==routeTemplate, flowNest: repId!==rep?.id}) }
       </React.Fragment>
    );
}

const OneSubRepView: React.FunctionComponent<ReportViewSubProps> = (
    {fxIdx,dispIdx,rep,repId,verId,parentOrc,surpId}
) => {
    const theme= useTheme();
    const orc=parentOrc['_OXIDESKIN_THICK_'+surpId];
    const [firstNode,_S]=useThreeColumnSubr({orc, config:config测量仪,parentOrc,slash:true,
                       href:`/report/OXIDESKIN_THICK/ver/${verId}/${repId}/${surpId}/Instrument`});
    const [render测量, headrows]=useRaftTableViewer(config测量, orc?.测量表,2,true, true, true);
    return <React.Fragment>
        <div css={{"@media print": {paddingBottom: '3.5rem', pageBreakInside: 'avoid'}}}>
            <Text variant="h2" css={{textAlign: 'center', marginTop: '1rem',
            }}>管壁氧化皮厚度测量报告{dispIdx ? '-' + dispIdx : ''}</Text>
            <div css={{display: 'flex', justifyContent: 'space-between'}}>
                <Text></Text>
                <Text>报告编号：{rep.isp.no}</Text>
            </div>
        </div>
        <Table fixed={ ["10%","30%","12%","10%","8.2%","%"] } tight miniw={800}
                  css={{borderCollapse: 'collapse', "@media print": {marginTop: '-3.5rem'}}}>
            <TableBody>
                {firstNode}
            </TableBody>
        </Table>
        <Table fixed={ ["62%","%"]  }  css={ {borderCollapse: 'collapse' } } tight  miniw={800}>
            <TableBody>
                <DirectLink  href={`/report/OXIDESKIN_THICK/ver/${verId}/${repId}/${surpId}/ResultDiagram`}>
                    <TableRow>
                        <Cell colSpan={2} split={true} css={{"@media print": {height: undefined}, padding: 0}}>
                            <div css={{minHeight: '2rem', whiteSpace: 'pre-wrap'}}>
                                <Text css={{fontSize: '0.8rem'}}>测厚点部位示意图：</Text><br/>
                                {orc?.示图说明}
                            </div>
                            {orc?._FILE_S部位?.map(({name, url}: any, i: number) => {
                                return <div key={i} css={{"@media print": {pageBreakInside: 'avoid'}}}>
                                    {i > 0 && <hr/>}
                                    <div css={{display: 'flex', justifyContent: 'space-around', alignItems: 'center',}}>
                                        {url &&
                                            <img src={process.env.REACT_APP_OSS_ENDP + url} alt={url}
                                                 css={{
                                                     maxHeight: '14cm',
                                                     maxWidth: '-webkit-fill-available',
                                                     [theme.mediaQueries.lg]: {maxHeight: '18cm', maxWidth: undefined},
                                                     "@media print": {maxHeight: '86vh', maxWidth: '100%'},
                                                 }}
                                            />
                                        }
                                    </div>
                                </div>
                            })}
                        </Cell>
                    </TableRow>
                </DirectLink>
            </TableBody>
        </Table>
        <Table fixed={ ["%","11.4%","13.3%","10%","16%","11.4%","13.3%","10%"] } tight miniw={800}
                   css={{borderCollapse: 'collapse', }}>
            <TableHead>
              <DirectLink href={`/report/OXIDESKIN_THICK/ver/${verId}/${repId}/${surpId}/EvaluationForm`}>
                  <TableRow>
                      {(new Array(2)).fill(null).map(( _,  b:number) => {
                          return <React.Fragment key={b}>
                              <CCell rowSpan={2}>{config测量[0][0]}</CCell>
                              <CCell colSpan={2}>实测厚度</CCell>
                              <CCell rowSpan={2}>{config测量[3][0]}（{orc?.壁单位??'mm'}）</CCell>
                          </React.Fragment>;
                      }) }
                  </TableRow>
                  <TableRow>
                      {(new Array(2)).fill(null).map(( _,  b:number) => {
                          return <React.Fragment key={b}>
                              <CCell>{config测量[1][0]}（{orc?.壁单位??'mm'}）</CCell><CCell>{config测量[2][0]}（{orc?.皮单位??'mm'}）</CCell>
                          </React.Fragment>;
                      }) }
                  </TableRow>
              </DirectLink>
            </TableHead>
            <TableBody>
                <DirectLink href={`/report/OXIDESKIN_THICK/ver/${verId}/${repId}/${surpId}/EvaluationForm`}>
                    {render测量}
                </DirectLink>
            </TableBody>
        </Table>
        <TailMenRowIspCheck orc={orc} rep={rep} href={`/report/OXIDESKIN_THICK/ver/${verId}/${repId}/${surpId}/ResultDiagram`}>
            <DirectLink href={`/report/OXIDESKIN_THICK/ver/${verId}/${repId}/${surpId}/ResultDiagram`}>
                <TableRow>
                    <Cell split={true} colSpan={2}><div css={{minHeight: '2rem', whiteSpace: 'pre-wrap'}}>
                        检测结果：<br/>
                        {orc?.检测结果 || '／'}
                    </div></Cell>
                </TableRow>
            </DirectLink>
        </TailMenRowIspCheck>
    </React.Fragment>;
}
